/*
 * Copyright (C) 2019 Peng fei Pan <panpfpanpf@outlook.me>
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package me.panpf.sketch.process;



import me.panpf.sketch.Sketch;
import me.panpf.sketch.cache.BitmapPool;
import me.panpf.sketch.decode.ResizeCalculator;
import me.panpf.sketch.request.Resize;
import me.panpf.sketch.util.Utils;
import me.panpf.sketch.zoom.PixmapBuild;
import ohos.agp.components.Image;
import ohos.agp.render.*;
import ohos.agp.utils.Color;
import ohos.media.image.PixelMap;
import ohos.media.image.common.PixelFormat;

/**
 * 圆形图片处理器
 */
@SuppressWarnings("WeakerAccess")
public class CircleImageProcessor extends WrappedImageProcessor {


    private static volatile CircleImageProcessor instance;

    public CircleImageProcessor(WrappedImageProcessor wrappedProcessor) {
        super(wrappedProcessor);
    }

    private CircleImageProcessor() {
        this(null);
    }

    public static CircleImageProcessor getInstance() {
        if (instance == null) {
            synchronized (CircleImageProcessor.class) {
                if (instance == null) {
                    instance = new CircleImageProcessor();
                }
            }
        }
        return instance;
    }

    @Override
    protected boolean isInterceptResize() {
        return true;
    }

    @Override
    public PixmapBuild onProcess(Sketch sketch, PixmapBuild bitmap, Resize resize, boolean lowQualityImage) {
        if (bitmap.getPixelMap().isReleased()) {
            return bitmap;
        }

        int targetWidth = resize != null ? resize.getWidth() : bitmap.getPixelMap().getImageInfo().size.width;
        int targetHeight = resize != null ? resize.getHeight() : bitmap.getPixelMap().getImageInfo().size.height;
        int newBitmapSize = targetWidth < targetHeight ? targetWidth : targetHeight;
        Image.ScaleMode scaleType = resize != null ? resize.getScaleType() : Image.ScaleMode.CENTER;

        ResizeCalculator resizeCalculator = sketch.getConfiguration().getResizeCalculator();
        ResizeCalculator.Mapping mapping = resizeCalculator.calculator(bitmap.getPixelMap().getImageInfo().size.width, bitmap.getPixelMap().getImageInfo().size.height,
                newBitmapSize, newBitmapSize, Utils.coverScaleMode(scaleType), resize != null && resize.getMode() == Resize.Mode.EXACTLY_SAME);


        PixelFormat config = lowQualityImage ? PixelFormat.UNKNOWN :  PixelFormat.ARGB_8888;
        BitmapPool bitmapPool = sketch.getConfiguration().getBitmapPool();

        PixmapBuild circleBitmap = bitmapPool.getOrMake(mapping.imageWidth, mapping.imageHeight, config);

        Canvas canvas = new Canvas( new Texture(circleBitmap.getPixelMap()));
        Paint paint = new Paint();
        paint.setAntiAlias(true);
        canvas.drawColor(Color.rgb(0,0,0),BlendMode.CLEAR);
        paint.setColor(new Color(0xFFFF0000));

        // 绘制圆形的罩子
        canvas.drawCircle((float) (mapping.imageWidth / 2d), (float) (mapping.imageHeight / 2d),
                (float) ((mapping.imageWidth < mapping.imageHeight ? mapping.imageWidth : mapping.imageHeight) / 2d), paint);

        // 应用遮罩模式并绘制图片
        paint.setBlendMode(BlendMode.SRC_IN);//BlendMode.DST_OUT
        canvas.drawPixelMapHolder(new PixelMapHolder(bitmap.getPixelMap()), -mapping.srcRect.left, -mapping.srcRect.top, paint);

        return circleBitmap;
    }

    @Override
    public String onToString() {
        return "CircleImageProcessor";
    }

    @Override
    public String onGetKey() {
        return "Circle";
    }
}
